package com.exercise.core.web.context;/**
 * Created by kete on 2017-05-05.
 */

import com.exercise.core.deploy.ModuleManager;
import com.exercise.core.util.WebUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

/**
 * 作者：kete
 * 创建时间：2017-05-05 15:31
 * 功能描述：
 * 版本：
 */
public class EbiddingContextLoaderListener extends ContextLoaderListener {

    private static Logger logger = LoggerFactory.getLogger(EbiddingContextLoaderListener.class);

    public static final String CONTEXT_NODE_NAME_PARAM = "contextNodeName";
    public static final String CONTEXT_WEBDOCS_PARAM = "contextWebdocs";
    public static final String CONTEXT_WEBDOCS_URL_PARAM = "contextWebdocsURL";
    public static final String CONTEXT_VIEWS_PARAM = "contextViews";
    public static final String CONTEXT_CONFIG_PREFIX_PARAM = "configPrefix";
    public static final String CONTEXT_CONFIG_SYSTEM_TYPE = "systemType";

    public EbiddingContextLoaderListener() {

    }

    public EbiddingContextLoaderListener(WebApplicationContext context) {
        super(context);
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        super.contextInitialized(event);

        ServletContext servletContext = event.getServletContext();

        String contextNodeName = servletContext.getInitParameter(CONTEXT_NODE_NAME_PARAM);
        String contextWebdocsDir = servletContext.getInitParameter(CONTEXT_WEBDOCS_PARAM);
        String contextWebdocsURL = servletContext.getInitParameter(CONTEXT_WEBDOCS_URL_PARAM);
        String contextViewsDir = servletContext.getInitParameter(CONTEXT_VIEWS_PARAM);
        String cntextConfigPrefix = servletContext.getInitParameter(CONTEXT_CONFIG_PREFIX_PARAM);
        String cntextConfigTystemType = servletContext.getInitParameter(CONTEXT_CONFIG_SYSTEM_TYPE);

        if (StringUtils.isNotEmpty(contextNodeName)) {
            WebUtil.nodeName = contextNodeName;
        } else {
            WebUtil.nodeName = "ebidding";
        }

        if (StringUtils.isNotEmpty(contextWebdocsDir)) {
            ModuleManager.webdocsDir = contextWebdocsDir;
        } else {
            ModuleManager.webdocsDir = servletContext.getRealPath("/");
        }

        if (StringUtils.isNotEmpty(contextWebdocsURL)) {
            WebUtil.webdocsURL = contextWebdocsURL;
            //WebUtil.isWebdocs = true;
        } else {
            WebUtil.webdocsURL = "/webdocs";
        }

        if (StringUtils.isNotEmpty(contextViewsDir)) {
            ModuleManager.viewsDir = contextViewsDir;
        } else {
            String webInfo = servletContext.getRealPath("/WEB-INF");
            ModuleManager.viewsDir = webInfo + "/views";
        }

        if (StringUtils.isNotEmpty(cntextConfigPrefix)) {
            WebUtil.configPrefix = cntextConfigPrefix;
        } else {
            WebUtil.configPrefix = "local";
        }

        logger.info(">>>>>应用节点名:" + WebUtil.nodeName);
        logger.info(">>>>>Webdocs:" + ModuleManager.webdocsDir);
        logger.info(">>>>>Views:" + ModuleManager.viewsDir );

        if(!StringUtils.equalsIgnoreCase(cntextConfigTystemType, "app"))
            ModuleManager.deploy();
    }
}
